/**
 * 订单 - 赠品信息页面
 * @author miaoyl
 * @date 2019/02/12 14:26
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            GWCSDOCNO:'',
            GWCS010:'',
            giftClick:false,//赠品编辑界面按钮disabled
        },
        callObj,
        moduleName,
        table,listTable,
        layOpenIndex
    ;

    var gwai710GiftEdit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai710_edit_giftInfo',
                _loadTplsFull: [{
                    dom: 'common_open_gwcs109', tpl: 'common/_open_gwcs109'
                },{
                    dom: 'common_open_gwdcz', tpl: 'common/_open_gwdcz'
                }]
            }, win);
        },
        initListTable: function () {
            //表格初始化
            layui.use(['table'], function () {
                table = layui.table;
                listTable = table.render({
                    elem: app.getByid(doc, 'gwai710_giftInfo_table'),
                    id: 'gwai710_giftInfo_table',
                    cols: [[
                        {field: 'GWCSZ003', type: 'checkbox', title: '选择'},
                        {field: 'GWCSZ007', width: 140, title: '活动编号', event: 'openGWCS109'},
                        {field: 'GWDC003', width: 100, title: '活动名称'},
                        {field: 'GWCSZ004', width: 80, title: '赠品品号', event: 'openGwdcz'},
                        {field: 'GWAML003_2', width: 80, title: '赠品品名'},
                        {field: 'GWAML004_2', width: 80, title: '赠品规格'},
                        {field: 'GWCSZ005', width: 80, title: '赠品数量', edit: 'text'},
                        {field: 'GWCSZ006', width: 80, title: '赠品价格'},
                        {field: 'GWCSZ008', width: 80, title: '单身品号'},
                        {field: 'GWAML003_6', width: 80, title: '单身品名'},
                        {field: 'GWAML004_6', width: 80, title: '单身规格'},
                        {field: 'GWCS003_SUM', width: 100, title: '订单活动总数量'},
                        {field: 'GWCS005_SUM', width: 100, title: '订单活动总金额'},
                        {field: 'GWDC026_MAX', width: 100, title: '本单最大赠品数量'},
                        {field: 'GWDC025_MAX', width: 100, title: '本单最大赠品金额'},
                        {field: 'GWDC020', width: 80, title: '起订数量'},
                        {field: 'GWDC021', width: 80, title: '起订金额'},
                        {field: 'GWDC026', width: 80, title: '最大赠品数量'},
                        {field: 'GWDC025', width: 80, title: '最大赠品金额'},
                        {field: 'GWCSZDOCNO', width: 80, title: '单号', hide: true},
                        {field: 'GWDC005', width: 80, title: '促销方式', hide: true},
                        {field: 'GWDC022', width: 80, title: '阶梯定价', hide: true}
                    ]],
                    page: {layout: ['prev', 'page', 'next', 'count']}
                });

                //监听事件
                table.on('tool(gwai710_giftInfo_table)', function (obj) {
                    switch (obj.event) {
                        case 'openGWCS109':
                            self._openTplFull({
                                tag: 'common_open_gwcs109',
                                tagData: {
                                    GWCSDOCNO: vueData.GWCSDOCNO,
                                    GWCS010: vueData.GWCS010,
                                    cb: function (data) {
                                        obj.update({
                                            'GWCSZ007': data.GWCS109,
                                            'GWDC003': data.GWDC003,
                                            'GWCSZ008': data.GWCS001,
                                            'GWAML003_6': data.GWAML003,
                                            'GWAML004_6': data.GWAML004,
                                            'GWCS003_SUM': data.GWCS003_SUM,
                                            'GWCS005_SUM': data.GWCS005_SUM,
                                            'GWDC020': data.GWDC020,
                                            'GWDC021': data.GWDC021,
                                            'GWDC022': data.GWDC022,
                                            'GWDC025': data.GWDC025,
                                            'GWDC026': data.GWDC026,
                                            'GWDC005': data.GWDC005,
                                            'GWDC026_MAX': data.GWDC026_MAX,
                                            'GWDC025_MAX': data.GWDC025_MAX,
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择活动',
                                    area: ['900px', '430px']
                                }
                            });
                            break;
                        case 'openGwdcz' :
                            self._openTplFull({
                                tag: 'common_open_gwdcz',
                                tagData: {
                                    GWCQ010: vueData.GWCS010,//经销商
                                    GWDC001: obj.data.GWCSZ007,//活动编号
                                    GWDC018: obj.data.GWCSZ008,//产品编号
                                    GWCS003_SUM: obj.data.GWCS003_SUM,//订单数量
                                    GWCS005_SUM: obj.data.GWCS005_SUM,//订单金额
                                    GWDC022: obj.data.GWDC022,//阶梯定价
                                    cb: function (data) {
                                        var count = obj.data.GWCSZ003;
                                        var listData = listTable.getDatas();
                                        listData.removeByMap({
                                            GWCSZ003: obj.data.GWCSZ003
                                        });
                                        for (var j = 0; j < data.length; j++) {
                                            listData.push({
                                                GWCSZ003: count,
                                                GWCSZ007: obj.data.GWCSZ007,
                                                GWDC003: obj.data.GWDC003,
                                                GWCSZ004: data[j].GWDCZ004,
                                                GWAML003_2: data[j].GWAML003,
                                                GWAML004_2: data[j].GWAML004,
                                                GWCSZ005: '',
                                                GWCSZ006: data[j].GWDCZ005,
                                                GWCSZ008: obj.data.GWCSZ008,
                                                GWAML003_6: obj.data.GWAML003_6,
                                                GWAML004_6: obj.data.GWAML004_6,
                                                GWCS003_SUM: obj.data.GWCS003_SUM,
                                                GWCS005_SUM: obj.data.GWCS005_SUM,
                                                GWDC020: obj.data.GWDC020,
                                                GWDC021: obj.data.GWDC021,
                                                GWDC025: obj.data.GWDC025,
                                                GWDC026: obj.data.GWDC026,
                                                GWCSZDOCNO: vueData.GWCSDOCNO,
                                                GWDC005: obj.data.GWDC005,
                                                GWDC025_MAX:obj.data.GWDC025_MAX,
                                                GWDC026_MAX:obj.data.GWDC026_MAX
                                            });
                                            count++;
                                            self.tableReload(listData);
                                        }
                                    }
                                },
                                index: 1,
                                openOpts: {
                                    title: '选择赠品信息',
                                    area: ['700px', '430px']
                                }
                            });
                            break;
                    }
                    ;
                });

                //监听单元格编辑
                table.on('edit(gwai710_giftInfo_table)', function (obj) {
                    var GWCS111 = "";
                    var listData = listTable.getDatas();
                    if (obj.field == 'GWCSZ005') {
                        app.ajax({
                            xurl: 'crm/gwai710/getGwcs111',
                            xdata: {GWCSDOCNO: vueData.GWCSDOCNO, GWCS109: obj.data.GWCSZ007},
                            async: false,
                            cb: function (result) {
                                if (result.ok) {
                                    GWCS111 = result.data.data.GWCS111;
                                }
                            }
                        });
                        var GWCSZ005 = obj.value;
                        if (obj.data.GWDC005 == '4') {//满量赠送
                            if (obj.data.GWDC026 != '' && obj.data.GWDC026 != null) {
                                var max_num = parseInt(obj.data.GWCS003_SUM / obj.data.GWDC020) * obj.data.GWDC026;
                                if (GWCS111 == "1") {//组合
                                    listData.forEach(function (item) {
                                        //组合+最大赠送量
                                        if (obj.data.GWCSZ007 == item.GWCSZ007 && obj.data.GWCSZ003 != item.GWCSZ003) {
                                            GWCSZ005 = math.add(GWCSZ005, item.GWCSZ005);
                                        }
                                    });

                                    if (GWCSZ005 > max_num) {
                                        obj.updateOfEdit({
                                            'GWCSZ005': ''
                                        });
                                        app.showWarning("数量过大，超出可赠范围！");
                                        return;
                                    } else {
                                         obj.updateOfEdit({
                                            'GWCSZ005': obj.value
                                         });
                                     }
                                }
                                if (GWCS111 == "2") {//非组合
                                    listData.forEach(function (item) {
                                        //非组合+最大赠送量
                                        if (obj.data.GWCSZ007 == item.GWCSZ007 && obj.data.GWCSZ008 == item.GWCSZ008 && obj.data.GWCSZ003 != item.GWCSZ003) {
                                            GWCSZ005 = math.add(GWCSZ005, item.GWCSZ005);
                                        }
                                    });

                                    if (GWCSZ005 > max_num) {
                                        obj.updateOfEdit({
                                            'GWCSZ005': ''
                                        });
                                        app.showWarning("数量过大，超出可赠范围！");
                                        return;
                                    } else {
                                         obj.updateOfEdit({
                                            'GWCSZ005': obj.value
                                         });
                                     }
                                }

                            } else {
                                var max_Money = parseInt(obj.data.GWCS003_SUM / obj.data.GWDC020) * obj.data.GWDC025;
                                var money = GWCSZ005 * obj.data.GWCSZ006;
                                if (GWCS111 == "1") {//组合
                                    listData.forEach(function (item) {
                                        //组合+最大赠送额
                                        if (obj.data.GWCSZ007 == item.GWCSZ007 && obj.data.GWCSZ003 != item.GWCSZ003) {
                                            money = math.add(money, math.mul(item.GWCSZ005, item.GWCSZ006));
                                        }
                                    });
                                    if (money > max_Money) {
                                        obj.updateOfEdit({
                                            'GWCSZ005': ''
                                        });
                                        app.showWarning("数量过大，超出可赠范围！");
                                        return;
                                    } else {
                                         obj.updateOfEdit({
                                            'GWCSZ005': obj.value
                                         });
                                     }
                                }
                                if (GWCS111 == "2") {//非组合
                                    listData.forEach(function (item) {
                                        //非组合+最大赠送额
                                        if (obj.data.GWCSZ007 == item.GWCSZ007 && obj.data.GWCSZ008 == item.GWCSZ008 && obj.data.GWCSZ003 != item.GWCSZ003) {
                                            money = math.add(money, math.mul(item.GWCSZ005, item.GWCSZ006));
                                        }
                                    });

                                    if (money > max_Money) {
                                        obj.updateOfEdit({
                                            'GWCSZ005': ''
                                        });
                                        app.showWarning("数量过大，超出可赠范围！");
                                        return;
                                    } else {
                                         obj.updateOfEdit({
                                            'GWCSZ005': obj.value
                                         });
                                     }
                                }

                            }
                        } else if (obj.data.GWDC005 == '3') {//满额赠送
                            if (obj.data.GWDC025 != '' && obj.data.GWDC025 != null) {//最大赠送金额
                                var max_Money = parseInt(obj.data.GWCS005_SUM / obj.data.GWDC021) * obj.data.GWDC025;
                                var money = GWCSZ005 * obj.data.GWCSZ006;
                                if (GWCS111 == "1") {//组合
                                    listData.forEach(function (item) {
                                        //组合+最大赠送额
                                        if (obj.data.GWCSZ007 == item.GWCSZ007 && obj.data.GWCSZ003 != item.GWCSZ003) {
                                            money = math.add(money, math.mul(item.GWCSZ005, item.GWCSZ006));
                                        }
                                    });

                                    if (money > max_Money) {
                                        obj.updateOfEdit({
                                            'GWCSZ005': ''
                                        });
                                        app.showWarning("数量过大，超出可赠范围！");
                                        return;
                                    } else {
                                         obj.updateOfEdit({
                                            'GWCSZ005': obj.value
                                         });
                                     }
                                }
                                if (GWCS111 == "2") {//非组合
                                    listData.forEach(function (item) {
                                        //非组合+最大赠送额
                                        if (obj.data.GWCSZ007 == item.GWCSZ007 && obj.data.GWCSZ008 == item.GWCSZ008 && obj.data.GWCSZ003 != item.GWCSZ003) {
                                            money = math.add(money, math.mul(item.GWCSZ005, item.GWCSZ006));
                                        }
                                    });

                                    if (money > max_Money) {
                                        obj.updateOfEdit({
                                            'GWCSZ005': ''
                                        });
                                        app.showWarning("数量过大，超出可赠范围！");
                                        return;
                                    } else {
                                         obj.updateOfEdit({
                                            'GWCSZ005': obj.value
                                         });
                                     }
                                }

                            } else {//最大赠送数量
                                var max_num = parseInt(obj.data.GWCS005_SUM / obj.data.GWDC021) * obj.data.GWDC026;
                                if (GWCS111 == "1") {//组合
                                    listData.forEach(function (item) {
                                        //组合+最大赠送量
                                        if (obj.data.GWCSZ007 == item.GWCSZ007 && obj.data.GWCSZ003 != item.GWCSZ003) {
                                            GWCSZ005 = math.add(GWCSZ005, item.GWCSZ005);
                                        }
                                    });

                                    if (GWCSZ005 > max_num) {
                                        obj.updateOfEdit({
                                            'GWCSZ005': ''
                                        });
                                        app.showWarning("数量过大，超出可赠范围！");
                                        return;
                                    } else {
                                         obj.updateOfEdit({
                                            'GWCSZ005': obj.value
                                         });
                                     }
                                }
                                if (GWCS111 == "2") {//非组合
                                    listData.forEach(function (item) {
                                        //非组合+最大赠送量
                                        if (obj.data.GWCSZ007 == item.GWCSZ007 && obj.data.GWCSZ008 == item.GWCSZ008 && obj.data.GWCSZ003 != item.GWCSZ003) {
                                            GWCSZ005 = math.add(GWCSZ005, item.GWCSZ005);
                                        }
                                    });

                                    if (GWCSZ005 > max_num) {
                                        obj.updateOfEdit({
                                            'GWCSZ005': ''
                                        });
                                        app.showWarning("数量过大，超出可赠范围！");
                                        return;
                                    } else {
                                         obj.updateOfEdit({
                                            'GWCSZ005': obj.value
                                         });
                                     }
                                }
                            }
                        }
                    }
                });
            });
        },
        tableReload: function (listData) {
            listTable.reload({
                data: listData
            });
        },
        addDataBatch: function () {
            var listData = listTable.getDatas();
            var count = 0;
            if(listData.length > 0){
                count = parseInt(listData[listData.length-1].GWCSZ003);
            }
            listData.push({
                GWCSZ003:count+1,
                GWCSZ007:'',
                GWDC003:'',
                GWCSZ004:'',
                GWAML003_2:'',
                GWAML004_2:'',
                GWCSZ005:'',
                GWCSZ006:'',
                GWCSZ008:'',
                GWAML003_6:'',
                GWAML004_6:'',
                GWCS003_SUM:'',
                GWCS005_SUM:'',
                GWDC020:'',
                GWDC021:'',
                GWDC025:'',
                GWDC026:'',
                GWCSZDOCNO:vueData.GWCSDOCNO,
                GWDC005:vueData.GWDC005,
            });
            self.tableReload(listData);
        },
        deleteDataBatch: function(){
            var listData = listTable.getDatas();
            var checkStatus = table.checkStatus('gwai710_giftInfo_table');
            if (checkStatus.data.length > 0) {
                checkStatus.data.forEach(function (v) {
                    listData.removeByMap({
                        GWCSZ003 : v.GWCSZ003
                    });
                });
                self.tableReload(listData);
            }
        },

        addData: function(listData,GWCSDOCNO){
            var listData = listTable.getDatas();
            /*if(listData.length < 1){
                app.showWarning('无赠品信息，请关闭或者维护赠品信息!');
                return;
            }*/
            for (var i = 0; i < listData.length; i++) {
                var v = listData[i];
                if (v.GWCSZ005 == '' || v.GWCSZ005 == null
                    || v.GWCSZ005 <=0 ) {
                    app.showWarning('存在赠品数量小于等于0的资料!');
                    return;
                }
                if (v.GWCSZ004 == '' || v.GWCSZ004 == null) {
                    app.showWarning('存在赠品编号为空的资料!');
                    return;
                }
                if (v.GWCSZ007 == '' || v.GWCSZ007 == null) {
                    app.showWarning('存在活动编号为空的资料!');
                    return;
                }
            }
            app.ajax({
                xurl: 'crm/gwai710/addGwcszData',
                xdata: {listData:listData,GWCSDOCNO:GWCSDOCNO},
                loading: true,
                toJsonStr: true,
                okmsg: moduleName +'保存成功',
                cb: function (result) {
                    if (result.ok) {
                        self.closePage();
                    }
                }
            });
        },
        closePage: function(){
            self._closeTpl();
            //layer.close(layOpenIndex);
        },
        _call: function (tagData) {
            moduleName = tagData.moduleName;
            layOpenIndex = tagData.layOpenIndex;
            vueData.GWCSDOCNO = tagData.GWCSDOCNO;
            vueData.GWCS010 = tagData.GWCS010;
            vueData.giftClick = tagData.giftClick;
            app.ajax({
                xurl: 'crm/gwai710/getGiftInfoData',
                xdata: {
                    GWCSDOCNO : tagData.GWCSDOCNO,
                    GWCS010 : tagData.GWCS010
                },
                cb: function (result) {
                    if (result.ok) {
                        self.tableReload(result.data.data);
                    }
                }
            });

        },
        _ready: function () {
            table = layui.table;
            var vue = new Vue({
                el: '#gwai710_edit_giftInfo',
                data: vueData,
                methods: {
                    addDataBatch: function () {
                        self.addDataBatch();
                    },
                    deleteDataBatch: function () {
                        self.deleteDataBatch();
                    },
                    sure: function () {
                        var listData = listTable.getDatas();
                        listData.addMap(vueData,true);
                        self.addData(listData,vueData.GWCSDOCNO);
                    },
                    cancel: function () {
                        self.closePage();
                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('gwai710_edit_giftInfo');
                }
            });
            self.initListTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new gwai710GiftEdit();

});